<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document-admin</title>
</head>
<!-- 引入样式 -->
<link rel="stylesheet" href="../../plugins/element-ui/lib/theme-chalk/index.css"/>
<!--<link rel="stylesheet" href="../../styles/common.css" />-->
<link rel="stylesheet" href="../../styles/page.css" />
<body>
<div class="dashboard-container" id="account-app">
    <div class="container">
        <div class="tableBar" style="justify-content: flex-start;">
            <el-input v-model="input" placeholder="请输入账户名" style="width: 250px" clearable @keyup.enter.native="handleQuery">
                <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="handleQuery"></i>
            </el-input>
            <div style="position: absolute;right: 28px">
                <el-button type="primary" @click="selectFlow()">
                    查看流水
                </el-button>
                <el-button type="primary" @click="addAccountHandle('add')">
                    + 添加账户
                </el-button>
            </div>
        </div>
        <!--管理员分页展示table-->
        <el-table :data="tableData" stripe class="tableBox">
            <el-table-column prop="name" label="账户名"></el-table-column>
            <el-table-column prop="count" label="金额"></el-table-column>
            <el-table-column prop="description" label="描述"></el-table-column>
            <el-table-column label="账号状态">
                <template slot-scope="scope">
                    {{ String(scope.row.status) === '0' ? '已禁用' : '正常' }}
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="160" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" class="blueBug" @click="addAccountHandle(scope.row.id)">
                        编辑
                    </el-button>
                    <el-button type="text" size="small" class="delBut non" @click="statusHandle(scope.row)" >
                        {{ scope.row.status == '1' ? '禁用' : '启用' }}
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--page-sizes 条目数 -->
        <el-pagination
                class="pageList"
                :page-sizes="[20,30,40,50]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                :current-page.sync="page"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
</div>
</body>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/lib/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/account.js"></script>
<script>
    new Vue({
        el: '#account-app',
        data() {
            return {
                input: '',
                counts: 0,
                page: 1,
                pageSize: 20,
                tableData : [],
                id : '',
                status : '',
            }
        },
        computed: {},

        /*vue初始化挂载函数*/
        created() {
            this.init();
            // alert(1439057691107037185);//js失去精度测试
            if(localStorage.getItem('userInfo')!=null){
                //获取当前登录管理员的账号，并赋值给模型数据
                this.user = JSON.parse(localStorage.getItem('userInfo')).username;
            }
        },
        mounted() {
        },
        methods: {
            async init () {
                const params = {
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input ? this.input : undefined
                };
                await getAccountList(params).then(res => {//res === res.data
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || [];
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            selectFlow(){
                window.parent.menuHandle({
                    id: '4',
                    url: '/back/page/account/flow.html',
                    name: '流水情况'
                },true)
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 添加&修改跳转
            addAccountHandle (st) {
                if (st === 'add'){
                    window.parent.menuHandle({
                        id: '5',
                        url: '/back/page/account/add.html',
                        name: '添加账户'
                    },true)
                } else {
                    window.parent.menuHandle({
                        id: '5',
                        url: '/back/page/account/add.html?id='+st,//跳转到add.html页面并携带一个id参数
                        name: '修改账户'
                    },true)
                }
            },
            //状态修改
            statusHandle (row) {
                this.id = row.id;
                this.status = row.status;
                this.$confirm('确认调整该账号的状态?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {//点击确定执行
                    // 调用请求方法   0--false----true   1---true---0
                    //this.status=="0"||this.status==0 ? "1" : "0"
                    enableOrDisableAccount({ 'id': this.id, 'status':!this.status?1:0 }).then(res => {//请求结果处理
                        console.log('enableOrDisable',res);
                        if (String(res.code) === '1') {
                            this.$message.success('账户状态更改成功！');
                            this.handleQuery()
                        }else{
                            this.$message.error(res.msg)
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            handleSizeChange (val) {
                this.pageSize = val;
                this.init()
            },
            handleCurrentChange (val) {
                this.page = val;
                this.init()
            }
        }
    })
</script>
</html>